package com.liyasong.cf;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.model.Preference;
import org.apache.mahout.cf.taste.model.PreferenceArray;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;


public class MysqlJdbc {
	String dbDriver = "com.mysql.jdbc.Driver";
	String url = "jdbc:mysql://localhost/grouplens";
	String username = "root";
	String password = "lysnhbgq01";
	
	Connection connection = null;
	Statement statement = null;
	ResultSet resultSet = null;
	
	String[] recommendedBooks;
	String[] recordBooks;
	
	public MysqlJdbc() {
		try{
			Class.forName(dbDriver).newInstance();
			connection = DriverManager.getConnection(url, username, password);
			statement = connection.createStatement();
			System.out.println("successful!");
		}catch(SQLException e) {
			System.out.println("db connected wrong" + e.getMessage());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public String[] getRecommendedBooks(List<RecommendedItem> recommendation) throws SQLException {
		int i = 0;
		String[] strs = new String[recommendation.size()];
		for (RecommendedItem recommendedItem : recommendation) {
			strs[i++] = getInfoByMovieId(recommendedItem.getItemID())+" 评分："+recommendedItem.getValue(); 
		}
		return strs;
	}
	/*
	public String[] getRecommendedBooks(long userid) throws SQLException {
		int i = 0;
		String[] strs = new String[recommendation.size()];
		for (RecommendedItem recommendedItem : recommendation) {
			strs[i++] = getInfoByMovieId(recommendedItem.getItemID())+" 评分："+recommendedItem.getValue(); 
		}
		return strs;
	}
	*/
	public String getInfoByBookId(long book_id) throws SQLException {
		String sql = "select book_name from recommend_book where book_id="+book_id;
		resultSet = statement.executeQuery(sql);
		resultSet.next();
		return resultSet.getString(1);
	}

	public String[] getRecordBooks(DataModel dataModel, long userId) throws TasteException, SQLException {
		PreferenceArray pa = dataModel.getPreferencesFromUser(userId);
		String[] strs = new String[pa.length()];
		int i = 0;
		for (Preference pre : pa) {
			strs[i++] = getInfoByBookId(pre.getItemID())+" 评分："+pre.getValue();
		}
		return strs;
	}


	public String getInfoByMovieId(long id) throws SQLException {
		String sql = "select name from movies where id="+id;
		resultSet = statement.executeQuery(sql);
		resultSet.next();
		return resultSet.getString(1);
	}
	
	public String[] getUserRecord(DataModel dataModel, long userId) throws TasteException, SQLException {
		PreferenceArray pa = dataModel.getPreferencesFromUser(userId);
		String[] strs = new String[pa.length()];
		int i = 0;
		for (Preference pre : pa) {
			strs[i++] = getInfoByMovieId(pre.getItemID())+" 评分："+pre.getValue();
		}
		return strs;
	}
	
	public String[] getRecommendInfo(List<RecommendedItem> recommendedItems) throws SQLException {
		int i = 0;
		String[] strs = new String[recommendedItems.size()];
		for (RecommendedItem recommendedItem : recommendedItems) {
			strs[i++] = getInfoByMovieId(recommendedItem.getItemID())+" 评分："+recommendedItem.getValue(); 
		}
		return strs;
	}
	public static void main(String[] args) throws SQLException{
		MysqlJdbc mj = new MysqlJdbc();
		System.out.println(mj.getInfoByBookId(363614839));
	}
}
